﻿using log4net;
using Lottery.Model;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace Lottery
{
    /// <summary>
    /// WinWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WinWindow : Window
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private DispatcherTimer timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 1) };
        private int timeClose = MyConfig.Instance.TimeClose <= 0 ? 5 : MyConfig.Instance.TimeClose;
        private static Random rand = new Random();
        public WinWindow(WinnerInfo winner)
        {
            InitializeComponent();
            this.timelable.Content = timeClose;
            this.timer.IsEnabled = true;
            timer.Tick += new EventHandler(timer_Tick);
            this.Cursor = System.Windows.Input.Cursors.None;
            this.init(winner);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timeClose--;
            this.timelable.Content = this.timeClose;
            if (this.timeClose <= 0)
                this.Close();
        }

        private void init(WinnerInfo winner)
        {
            this.Background = MyResources.GetImageBrush("win.jpg");

            //2017-01-12 update by xiaofeng
            int mywidth1 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; //屏幕宽度 
            int myheight1 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; //屏幕高度
            this.Width = mywidth1 / 2;
            this.Height = myheight1 / 2 - 45;
 

            MainGrid.Children.Add(this.getOneLable(winner.StaffName, 0));
            MainGrid.Children.Add(this.getOneLable(winner.JobNo, 1));
            MainGrid.Children.Add(this.getOneLable(winner.StaffDept, 2));
            MainGrid.Children.Add(this.getOneLable(winner.GiftLevelName, -1));
            MainGrid.Children.Add(this.getOneLable(winner.GiftName, -2));
        }

        private Label getOneLable(String content, int level)
        {
            Label l = new Label();
            l.Width = content.ToCharArray().Length * 60;
            l.Height = 60;
            //l.Name = "lable" + rand.Next(1000);
            l.Margin = this.count(this.Width, this.Height, l.Width, l.Height, level);
            l.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            l.FontFamily = new FontFamily("FZYaoTi");
            l.FontSize = 35;
            l.FontWeight = FontWeights.Black;
            l.Content = content;
            return l;
        }

        private Thickness count(double allw, double allh, double width, double height, int level)
        {
            double w = allw / 2 - width / 2;
            double h = allh / 2 - height / 2;
            double x = (height + 5) * level;
            return new Thickness(w, h + x, w, h - x);
        }
    }
}
